package com.lingxu.base.system.manage.controller;

import com.lingxu.base.common.api.vo.Result;
import com.lingxu.base.system.manage.entity.*;
import com.lingxu.base.system.manage.service.ISysUserMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Api(tags="用户菜单信息")
@RestController
@RequestMapping("/system/manage/sysusermenu")
public class SysUserMenuController {

    @Resource
    private ISysUserMenuService sysUsermenuService;



    /**
     * 添加
     *
     * @param sysUserMenu
     * @return
     */
    @ApiOperation(value="用户菜单信息-添加", notes="用户菜单信息-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody SysUserMenu sysUserMenu) {
        sysUsermenuService.save(sysUserMenu);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param sysUserMenu
     * @return
     */
    @ApiOperation(value="用户菜单信息-编辑", notes="用户菜单信息-编辑")
    @PostMapping(value = "/edit")
    public Result<?> edit(@RequestBody SysUserMenu sysUserMenu) {
        sysUsermenuService.updateById(sysUserMenu);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id 角色id
     * @return
     */
    @ApiOperation(value="用户菜单信息-通过id删除", notes="用户菜单信息-通过id删除")
    @PostMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id")@ApiParam(value = "id") String id) {
        sysUsermenuService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids 一批角色id
     * @return
     */
    @ApiOperation(value="用户菜单信息-批量删除", notes="用户菜单信息-批量删除")
    @PostMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids")@ApiParam(value = "id（多个用逗号','隔开）") String ids) {
        if(StringUtils.isNotBlank(ids)){
            this.sysUsermenuService.removeByIds(Arrays.asList(ids.split(",")));
            return Result.ok("批量删除成功！");
        }else{
            return Result.error("未传待删除数据");
        }
    }

    @ApiOperation(value="用户菜单信息-给用户分发菜单", notes="用户菜单信息-给用户分发菜单")
    @PostMapping(value = "/distributeUserMenus")
    public Result<?> distributeUserMenus(@RequestBody SysUserMenuVos vos) {
        sysUsermenuService.distributeUserMenus(vos);
        return Result.ok("用户分发菜单成功!");
    }


    @ApiOperation(value="用户菜单信息-根据用户获取菜单树（包含已获取和未获取）", notes="用户菜单信息-根据用户获取菜单树（包含已获取和未获取）")
    @PostMapping(value = "/queryMenuTreesByUserid")
    public Result<?> queryMenuTreesByUserid(@RequestParam(name="userid")@ApiParam(value = "用户id") String userid) {
        Result result = new Result();
        MenuTrees tree = new MenuTrees();
        List<MenuTree> list1 = sysUsermenuService.queryExistMenuTreeByUserid(userid);
        List<MenuTree> list2 = sysUsermenuService.queryNotExistMenuTreeByUserid(userid);
        tree.setList1(list1);
        tree.setList2(list2);
        result.setResult(tree);
        result.setCode(200);
        result.setSuccess(true);
        return result;
    }
    @ApiOperation(value="用户菜单信息-根据用户获取菜单树（已获取）", notes="用户菜单信息-根据用户获取菜单树（已获取）")
    @PostMapping(value = "/queryExistMenuTreesByUserid")
    public Result<?> queryExistMenuTreesByUserid(@RequestParam(name="userid")@ApiParam(value = "用户id") String userid) {
        Result result = new Result();
        List<MenuTree> list1 = sysUsermenuService.queryExistMenuTreeByUserid(userid);
        result.setResult(list1);
        result.setCode(200);
        result.setSuccess(true);
        return result;
    }
    @ApiOperation(value="用户菜单信息-根据用户获取菜单树（未获取）", notes="用户菜单信息-根据用户获取菜单树（未获取）")
    @PostMapping(value = "/queryNotExistMenuTreesByUserid")
    public Result<?> queryNotExistMenuTreesByUserid(@RequestParam(name="userid")@ApiParam(value = "用户id") String userid) {
        Result result = new Result();
        List<MenuTree> list1 = sysUsermenuService.queryNotExistMenuTreeByUserid(userid);
        result.setResult(list1);
        result.setCode(200);
        result.setSuccess(true);
        return result;
    }
}
